/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package network;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Vector;

/**
 *
 * @author Stein
 */
public class ReadWriteTCPConnectionToServer extends Thread {

  private Socket verbinding;
  private PrintWriter schrijver;
  private BufferedReader lezer;
  public String history = "";
  public Vector<String> connectedPeople = new Vector<String>(10);

  public ReadWriteTCPConnectionToServer(Socket s) {
    verbinding = s;
    try {
      this.lezer = new BufferedReader(new InputStreamReader(s.getInputStream()));
      this.schrijver = new PrintWriter(s.getOutputStream(), true);
    } catch (Exception e) {
      e.printStackTrace();
    }
    this.start();
  }

  @Override
  public void run() {
    handleData();
  }

  public void sendData(String data) {
    System.out.println("To server: " + data);
    schrijver.println(data);
  }

  private void handleData() {
    String msgReceived;
    try {
      while ((msgReceived = lezer.readLine()) != null) {
        System.out.println("From server: " + msgReceived);
        String[] tmp = msgReceived.split(":");

        //On new user
        if (tmp[0].equals("newuser")) {
          tmp[1] = tmp[1].trim();
          connectedPeople.add(tmp[1]);
          history += getTimestamp() + tmp[1] + " joined the channel\r\n";
        }

        //On join get other connected people
        if(tmp[0].equals("users")){
            for(int i=1;i<tmp.length;i++)
                connectedPeople.add(tmp[i]);
        }

        //On nickchange
        if (tmp[0].equals("nickname")) {

          tmp[1] = tmp[1].trim(); //nieuwe naam
          tmp[2] = tmp[2].trim(); //oude naam

          for (int i = 0; i < connectedPeople.size(); i++) {
            if (connectedPeople.get(i).trim().equals(tmp[2])) {
              connectedPeople.set(i, tmp[1]);
            }
          }
          history += getTimestamp() + tmp[2] + " is now known as " + tmp[1] + "\r\n";
        }

        //On quit
        if (tmp[0].equals("userleft")) {
          tmp[1] = tmp[1].trim();
          connectedPeople.remove(tmp[1]);
          history += getTimestamp() + tmp[1] + " left the channel\r\n";
        }
      }
    } catch (IOException ex) {
      ex.printStackTrace();
    }
  }

  public String getTimestamp(){
    Date today = new Date();
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.setTime(today);
    int h = calendar.get(Calendar.HOUR_OF_DAY);
    int m = calendar.get(Calendar.MINUTE);
    int s = calendar.get(Calendar.SECOND);
    String timestamp = "[";
    if (h < 10) timestamp += "0" + h;
    else timestamp += h;
    timestamp += ":";
    if (m < 10) timestamp += "0" + m;
    else timestamp += m;
    timestamp += ":";
    if (s < 10) timestamp += "0" + s;
    else timestamp += s;
    timestamp += "] ";
    return timestamp;
  }
}
